package com.eascs.p2p.admin.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.admin.manager.IMsgSmsManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.MsgSmsEntity;
import com.eascs.p2p.common.vo.MsgSmsQueryVO;

@Repository
public class MsgSmsManagerImpl extends BaseDao implements IMsgSmsManager {

	@Override
	public boolean add(MsgSmsEntity sms) {
		Date date = new Date();
		sms.setCreateTime(date);
		sms.setUpdateTime(date);
		return this.insert(sms);
	}

	@Override
	public boolean update(MsgSmsEntity sms) {
		Date date = new Date();
		sms.setUpdateTime(date);
		return this.updateById(sms);
	}

	@Override
	public boolean updateAuthCodeStatus(String phone, String authCode, byte status) {
		String sql = "update  p2p_msg_sms s set s.sms_status = ? where s.sms_type=0 and s.phone_receiver=? and s.sms_content=? ";
		return this.execute(sql, new Object[] { status, phone, authCode });
	}

	@Override
	public MsgSmsEntity queryAuthCode(String phone, String authCode) {
		String sql = "select * from p2p_msg_sms s where s.sms_type=0 and s.phone_receiver=? and s.sms_content=?";
		return this.findEntity(sql, MsgSmsEntity.class, new Object[] { phone, authCode });
	}

	@Override
	public MsgSmsEntity query(MsgSmsQueryVO vo) {
		return this.findEntity(buildSql(vo), MsgSmsEntity.class, buildParams(vo));
	}

	@Override
	public int count(MsgSmsQueryVO vo) {
		String countSql = StringUtil.format("select count(1) from ({}) t__", buildSql(vo));
		return this.findInteger(countSql, buildParams(vo));
	}

	@Override
	public PageResult<Object[]> page(MsgSmsQueryVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder("select sms.id,item.item_name as sms_type,template_code,phone_receiver,sms_content,item1.item_name as sms_status from p2p_msg_sms sms ");
		sql.append("LEFT JOIN p2p_datadic_item item ON item.item_code = sms.sms_type AND item.dd_no = 12 ");
		sql.append("LEFT JOIN p2p_datadic_item item1 ON item1.item_code = sms.sms_status AND item1.dd_no = 13 ");
		sql.append( "where 1=1 ");
       
		if (StringUtils.isNotBlank(vo.getTemplateCode())) {
			
		    sql.append(" and template_code like ?");
		    sqlPageQuery.addParam("%" + vo.getTemplateCode()+ "%");
		}
	
		if (StringUtils.isNotBlank(vo.getPhone())) {
			sql.append(" and phone_receiver like ?");
			sqlPageQuery.addParam("%" +vo.getPhone()+ "%");
		}
		
		if (StringUtils.isNotBlank(vo.getSmsContent())) {
			sql.append(" and sms_content=?");
			sqlPageQuery.addParam(vo.getSmsContent());
		}
		if (StringUtils.isNotBlank(vo.getSmsStatus())) {
			sql.append(" and sms.sms_status=?");
			sqlPageQuery.addParam(vo.getSmsStatus());
		}
		
		if (StringUtils.isNotBlank(vo.getSmsType())) {
			sql.append(" and sms.sms_type=?");
			sqlPageQuery.addParam(vo.getSmsType());
		}
		if (StringUtils.isNotBlank(vo.getOrderby())) {
			sql.append(" order by ? ");
			sqlPageQuery.addParam(vo.getOrderby());
			if (StringUtils.isNotBlank(vo.getOrder())) {
				sql.append(" ? ");
				sqlPageQuery.addParam(vo.getOrder());
			}
		} else {
			sql.append(" order by create_time desc ");
		}
		sqlPageQuery.setQuerySql(sql.toString());

		return this.findDataWithPage(sqlPageQuery);
	}

	private static String buildSql(MsgSmsQueryVO vo) {
		StringBuilder sql = new StringBuilder("select * from p2p_msg_sms s where 1=1 ");

		if (StringUtils.isNotBlank(vo.getTemplateCode())) {
			sql.append(" and s.template_code=?");
		}
		if (StringUtils.isNotBlank(vo.getPhone())) {
			sql.append(" and s.phone_receiver=?");
		}
		if (StringUtils.isNotBlank(vo.getSmsContent())) {
			sql.append(" and s.sms_content=?");
		}
		if (StringUtils.isNotBlank(String.valueOf(vo.getSmsStatus()))) {
			sql.append(" and s.sms_status=?");
		}

		sql.append(" and TIMESTAMPADD(MINUTE, failure_mins, create_time)>now()");
		return sql.toString();
	}

	private static List<Object> buildParams(MsgSmsQueryVO vo) {
		List<Object> params = new ArrayList<Object>();

		if (StringUtils.isNotBlank(vo.getTemplateCode())) {
			params.add(vo.getTemplateCode());
		}
		if (StringUtils.isNotBlank(vo.getPhone())) {
			params.add(vo.getPhone());
		}
		if (StringUtils.isNotBlank(vo.getSmsContent())) {
			params.add(vo.getSmsContent());
		}
		if (StringUtils.isNotBlank(String.valueOf(vo.getSmsStatus()))) {
			params.add(vo.getSmsStatus());
		}

		return params;
	}

	@Override
	public int countPerHour(String phone) {
		String sql = "select * from p2p_msg_sms s where s.phone_receiver=? and to_days(s.create_time)=to_days(now()) "
				+ " and hour(s.create_time)=hour(now())";
		sql = StringUtil.format("select count(1) from ({}) t__", sql);
		return this.findInteger(sql, phone);
	}

	@Override
	public int countPerDay(String phone) {
		String sql = "select * from p2p_msg_sms s where s.phone_receiver=? and to_days(s.create_time)=to_days(now())";
		sql = StringUtil.format("select count(1) from ({}) t__", sql);
		return this.findInteger(sql, phone);
	}

	@Override
	public boolean isValid(String phone, String authCode) {
		String sql = "select * from p2p_msg_sms s where s.phone_receiver=? and s.sms_content=? "
				+ " and s.sms_status=0 and s.sms_type=0 "
				+ " and to_days(s.create_time)=to_days(now()) and TIMESTAMPADD(MINUTE, failure_mins, create_time)>now()";
		MsgSmsEntity sms = this.findEntity(sql, MsgSmsEntity.class, phone, authCode);
		return null == sms ? false : true;
	}

}
